const API_BASE_URL1 = 'http://192.168.124.20:8888'
const API_BASE_URL2 = 'https://p2.xuzhouzhihui.com/api'
import store from '@/store'
import { ACCESS_TOKEN } from '@/store/mutaction-types.js'

export default function request(api, method, data) {
	let header = {
		'Content-Type': 'application/json'
	}
	const token = uni.getStorageSync(ACCESS_TOKEN) || ''
	return new Promise((reslove, reject) => {
		uni.request({
			url: API_BASE_URL2 + api,
			method: method || 'GET',
			header: {
				'Content-Type': 'application/json',
				'Authorization': token
			},
			data: data || {},
			success: res => {
				const data = res.data
				if (data.code === 401) {
					uni.showModal({
						title: "提示",
						content: "登陆过期",
						showCancel: false,
						success: config => {
							if (config.confirm) {
								store.dispatch('Logout').then(res => {
									uni.redirectTo({
										url: '/packageC/login/login'
									})
								}).catch(error => {
									uni.$u.toast(error)
								})
							}
						}
					})
				} else if (data.code === 200) {
					reslove(res)
				} else {
					reject(data.msg || '服务器内部错误')
				}
			},
			fail: err => {
				reject(err)
			}
		})
	})
}

['OPTIONS', 'GET', 'POST'].forEach((method) => {
	request[method] = (api, data) => request(api, method, data)
})
